package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import model.Process;
import model.ProductType;
import service.Service;

public class AddEditType extends JFrame {
    private final Controller controller = new Controller();
    private Component owner;
    private JTextField txfProductName;
    private JList lstSelectedProcesses, lstAllProcesses;
    private JButton btnAddProcess, btnRemoveProcess, btnSave, btnCancel;
    private DefaultListModel dlmSelected;
    private ProductType productType = null;

    public AddEditType(Component owner, ProductType pt) {
	this(owner);
	this.productType = pt;

	fillData();
    }

    public AddEditType(Component owner) {
	this.owner = owner;
	setSize(567, 461);
	setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	setLocation(100, 100);
	setResizable(false);
	setVisible(true);
	setTitle("Add/Edit ProductType");
	getContentPane().setLayout(new BorderLayout(0, 0));

	JPanel panel = new JPanel();
	FlowLayout flowLayout = (FlowLayout) panel.getLayout();
	flowLayout.setAlignment(FlowLayout.LEFT);
	getContentPane().add(panel, BorderLayout.NORTH);

	JLabel lblName = new JLabel("Name");
	panel.add(lblName);

	txfProductName = new JTextField();
	panel.add(txfProductName);
	txfProductName.setColumns(10);

	JPanel panel_1 = new JPanel();
	getContentPane().add(panel_1, BorderLayout.WEST);
	panel_1.setLayout(new GridLayout(10, 1, 0, 0));

	btnAddProcess = new JButton("Add process");
	panel_1.add(btnAddProcess);

	btnRemoveProcess = new JButton("Remove process");
	panel_1.add(btnRemoveProcess);

	JPanel panel_2 = new JPanel();
	FlowLayout flowLayout_1 = (FlowLayout) panel_2.getLayout();
	flowLayout_1.setAlignment(FlowLayout.RIGHT);
	getContentPane().add(panel_2, BorderLayout.SOUTH);

	btnSave = new JButton("Save");
	panel_2.add(btnSave);

	btnCancel = new JButton("Cancel");
	panel_2.add(btnCancel);

	JPanel panel_3 = new JPanel();
	getContentPane().add(panel_3, BorderLayout.CENTER);
	panel_3.setLayout(new GridLayout(0, 2, 5, 0));

	JPanel panel_4 = new JPanel();
	panel_3.add(panel_4);
	panel_4.setLayout(new BorderLayout(0, 0));

	JPanel panel_7 = new JPanel();
	panel_4.add(panel_7, BorderLayout.NORTH);

	JLabel lblProductProcesses = new JLabel("Product processes");
	panel_7.add(lblProductProcesses);

	JScrollPane scrollPane = new JScrollPane();
	panel_4.add(scrollPane, BorderLayout.CENTER);

	dlmSelected = new DefaultListModel();

	lstSelectedProcesses = new JList(dlmSelected);
	scrollPane.setViewportView(lstSelectedProcesses);

	JPanel panel_5 = new JPanel();
	panel_3.add(panel_5);
	panel_5.setLayout(new BorderLayout(0, 0));

	JPanel panel_6 = new JPanel();
	FlowLayout flowLayout_2 = (FlowLayout) panel_6.getLayout();
	flowLayout_2.setAlignment(FlowLayout.LEFT);
	panel_5.add(panel_6, BorderLayout.NORTH);

	JLabel lblAvailableProcesses = new JLabel("Available processes");
	panel_6.add(lblAvailableProcesses);

	JScrollPane scrollPane_1 = new JScrollPane();
	panel_5.add(scrollPane_1, BorderLayout.CENTER);

	lstAllProcesses = new JList(Service.getInstance().getProcesses()
		.toArray());
	scrollPane_1.setViewportView(lstAllProcesses);

	btnAddProcess.addActionListener(controller);
	btnRemoveProcess.addActionListener(controller);
	btnSave.addActionListener(controller);
    }

    public void fillData() {
	if (productType == null)
	    return;

	txfProductName.setText(productType.getName());

	for (Process p : productType.getRecipe()) {
	    dlmSelected.addElement(p);
	}
    }

	private class Controller implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == btnAddProcess) {
				Object sel = lstAllProcesses.getSelectedValue();

				if (sel != null)
					dlmSelected.addElement(sel);
			} else if (e.getSource() == btnRemoveProcess) {
				int index = lstSelectedProcesses.getSelectedIndex();

				if (index >= 0)
					dlmSelected.remove(index);
			} else if (e.getSource() == btnSave) {
				String name = txfProductName.getText();
				ArrayList<Object> procsObjects = new ArrayList<Object>(
						Arrays.asList(dlmSelected.toArray()));

				if(procsObjects.size() > 0){
					Process[] p = new Process[procsObjects.size()];
					procsObjects.toArray(p);
	
					ArrayList<Process> processes = new ArrayList<Process>(
							Arrays.asList(p));
	
					ProductType result = null;
	
					if (productType == null) {
						result = Service.getInstance().addProductType(name,
								processes);
					} else {
						result = Service.getInstance().editProductType(productType,
								name, processes);
					}
	
					if (result != null) {
						AddEditType.this.dispose();
					} else {
						JOptionPane.showMessageDialog(AddEditType.this,
								"Type could not be created", "Error",
								JOptionPane.ERROR_MESSAGE);
					}
					if (owner instanceof ManageData) {
						((ManageData) owner).updateLists();
					}
				}
				else{
					JOptionPane.showMessageDialog(AddEditType.this,
							"Type should contain at least one process", "Error",
							JOptionPane.ERROR_MESSAGE);
				}
			}
		}
    }
}
